package org.jeecg.modules.ParameterModule.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.ParameterModule.service.AgainEnabledService;
import org.jeecg.modules.ParameterModule.service.CategoryInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 再次启用配置
 * */
@Api(tags="再次启用配置")
@RestController
@RequestMapping("/again/Enabled/parameter")
@Slf4j
public class AgainEnabledController {
    //再次启用
    @Autowired
    private AgainEnabledService againEnabledService;

    /**
     * 启用品类草稿件
     * @param
     * @return boolean
     */
    @PostMapping(value = "/enable_category_draft")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> enable_Category_draft(@RequestBody Map<String,Object> params) {
        String category_id = params.get("category_id").toString();//获取品类id
        int result = againEnabledService.enabled_category_caogao(category_id);
        if (result == 0) {
            return Result.ok("启用成功！");
        } else if(result==1){
            return Result.ok("启用成功，请添加参数数据");
        }else if(result==2){
            return Result.error("请先下架该品类再启用");
        }else if(result==4){
            return Result.error("启用失败");
        }else if(result==10){
            return Result.ok("再次启用成功！");
        }
        else {
            return Result.error("启用失败");
        }
    }


    /**
     * 正式品类还原-成草稿件
     * @param
     * @return boolean
     */
    @PostMapping(value = "/rewrite_restore_caogao")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> rewrite_restore_caogao(@RequestBody Map<String,Object> params) {
        String testcategory_id = params.get("category_id").toString();//获取测试品类id
        int result = againEnabledService.rewrite_category_caogao(testcategory_id);
        if (result == 0) {
            return Result.ok("还原成功！");
        } else if(result==1){
            return Result.ok("还原成功，请设置参数数据");
        }else if(result==2){
            return Result.error("还原失败，该数据异常");
        }
        else {
            return Result.error("还原失败，请联系管理员");
        }
    }


    /**
     * 启用版号草稿件
     * @param
     * @return boolean
     */
    @PostMapping(value = "/enable_plate_draft")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> enable_plate_draft(@RequestBody Map<String,Object> params) {
        String category_id = params.get("category_id").toString();//获取品类id
        int result = againEnabledService.enabled_plate_caogao(category_id);
        if (result == 0) {
            return Result.ok("启用成功！");
        } else if(result==1){
            return Result.ok("启用成功，请添加参数数据");
        }else if(result==2){
            return Result.error("请先下架该版号再启用");
        }else if(result==4){
            return Result.error("启用失败");
        }else if(result==10){
            return Result.ok("再次启用成功！");
        }
        else {
            return Result.error("启用失败");
        }
    }

    /**
     * 版号同步品类下的所有参数
     * @param
     * @return boolean
     */
    @PostMapping(value = "/synchronization_parameter")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result<?> synchronization_parameter(@RequestBody Map<String,Object> params) {
        String plate_id = params.get("category_id").toString();//获取版号id
        String category_id = params.get("top_id").toString();//品类id
        int result = againEnabledService.synchronization_parameter(category_id,plate_id);
        if (result == 0) {
            return Result.ok("同步数据成功！");
        }
        else {
            return Result.error("同步数据失败");
        }
    }

}
